<?php

namespace app\storeapi\model;
use app\common\model\Region as RegionModel;
use think\Db;


/**
 *商户模型类
 * Class Shop
 * @package app\storeapi\model
 */
class Shop extends Base
{

    protected $name = 'store_shop';

    protected $hiden = ['create_time','update_time'];
    /**
     * 追加字段
     * @var array
     */
    protected $append = ['region'];

    /**
     * 门店主页
     * @param $wxapp_id
     * @return \app\storeapi\model\store\Shop|null
     * @throws \think\exception\DbException
     */
    public function index($wxapp_id)
    {
        //商家中心基本信息
        $shop_id = $this->where(['wxapp_id'=>$wxapp_id,'is_delete'=>0])->value('shop_id')?:0;
        //订单
        $order = $this->totalOrder($wxapp_id);
        //访客记录
        $visitor = $this->visitor($wxapp_id);
        $data = compact('order','visitor','shop_id');
        return $data;
    }

    /**
     * 门店
     * @param $wxapp_id
     * @return \app\storeapi\model\store\Shop|null
     * @throws \think\exception\DbException
     */
    public function shop($shop_id)
    {
        //商家中心基本信息
        $info = $this->alias('a')
            ->join('yoshop_upload_file b','a.logo_image_id=b.file_id','left')
            ->field('a.shop_id,a.shop_name,a.phone,b.storage,b.file_url,b.file_name,a.province_id,a.city_id,a.region_id,a.address')
            ->where(['a.shop_id'=>$shop_id])
            ->find();
        if($info){
            //处理图片
            $info['image'] = $this->getFilePathAttr($info);
            $info['address'] = $info['region']['province'].$info['region']['city'].$info['region']['region'].$info['address'];
        }else{
            $info = [];
        }
        return $info;
    }


    //订单记录
    private function totalOrder($wxapp_id){
        // 筛选条件
        $filter = [];
        $filter['wxapp_id'] = $wxapp_id;
        $filter['pay_status'] = self::ORDER_PAY_SUCCESS;
        $order = db('order')
            ->where($filter)
            ->field(['sum(pay_price)' => 'total','count(*)'=> 'count'])
            ->whereTime('pay_time','today')
            ->find();

        $giftsorder = db('gifts_order')
            ->where($filter)
            ->field(['sum(pay_price)' => 'total','count(*)'=> 'count'])
            ->whereTime('pay_time','today')
            ->find();

        $ordertotal = $order['total']?:0;
        $giftsordertotal = $giftsorder['total']?:0;
        $order['total'] = bcadd($ordertotal,$giftsordertotal,2);

        $ordercount = $order['count']?:0;
        $giftsordercount = $giftsorder['count']?:0;
        $order['count'] = bcadd($ordercount,$giftsordercount,0);

        
        return $order;
    }

    //访客记录
    private function visitor($wxapp_id){
        //累计访客
        $allCount = db('store_shop_visitor')->where(['wxapp_id'=>$wxapp_id])->count();
        //近7天访客
        $sevenCount = db('store_shop_visitor')->where(['wxapp_id'=>$wxapp_id])->whereTime('create_time','-7 days')->count();
        //昨日访客
        $yesCount = db('store_shop_visitor')->where(['wxapp_id'=>$wxapp_id])->whereTime('create_time','yesterday')->count();

        return compact('allCount','sevenCount','yesCount');
    }


    /**
     * 店铺信息
     * @param $wxapp_id
     * @param $shop_id
     * @return \app\storeapi\model\store\Shop|null
     * @throws \think\exception\DbException
     */
    public function storeInfo($shop_id)
    {
        return static::get($shop_id, ['logo']);
    }

    /**
     * 地区名称
     * @param $value
     * @param $data
     * @return array
     */
    public function getRegionAttr($value, $data)
    {
        return [
            'province' => RegionModel::getNameById($data['province_id']),
            'city' => RegionModel::getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? '' : RegionModel::getNameById($data['region_id']),
        ];
    }
    /**
     * 关联文章封面图
     * @return \think\model\relation\HasOne
     */
    public function logo()
    {
        return $this->hasOne("UploadFile", 'file_id', 'logo_image_id');
    }

    /**
     * 编辑店铺信息
     * @param $params
     * @return \app\storeapi\model\store\Shop|null
     * @throws \think\exception\DbException
     */
    public function editInfo($param)
    {
        return $this->allowField(true)
            ->isUpdate(true,['wxapp_id'=>$param['wxapp_id'],'shop_id'=>$param['shop_id']])
            ->save($param);
    }



}
